//
//  GCZAFNetWorking.m
//  kdxyhd
//
//  Created by Macey Chen on 16/8/11.
//  Copyright © 2016年 zjw. All rights reserved.
//

#import "GCZAFNetWorking.h"

@implementation GCZAFNetWorking
+ (AFSecurityPolicy *)customSecurityPolicy
{
    //先导入证书，找到证书的路径
    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"ca" ofType:@"cer"];
    NSData *certData = [NSData dataWithContentsOfFile:cerPath];
    
    //AFSSLPinningModeCertificate 使用证书验证模式
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    //allowInvalidCertificates 是否允许无效证书（也就是自建的证书），默认为NO
    //如果是需要验证自建证书，需要设置为YES
    securityPolicy.allowInvalidCertificates = YES;
    
    //validatesDomainName 是否需要验证域名，默认为YES；
    //假如证书的域名与你请求的域名不一致，需把该项设置为NO；如设成NO的话，即服务器使用其他可信任机构颁发的证书，也可以建立连接，这个非常危险，建议打开。
    //置为NO，主要用于这种情况：客户端请求的是子域名，而证书上的是另外一个域名。因为SSL证书上的域名是独立的，假如证书上注册的域名是www.google.com，那么mail.google.com是无法验证通过的；当然，有钱可以注册通配符的域名*.google.com，但这个还是比较贵的。
    //如置为NO，建议自己添加对应域名的校验逻辑。
    securityPolicy.validatesDomainName = NO;
    NSSet *set = [[NSSet alloc] initWithObjects:certData, nil];
    securityPolicy.pinnedCertificates = set;
    
    return securityPolicy;
}

// Post 请求
+ (void)POST:(NSString *)URL params:(NSDictionary * )params success:(void (^)(id response))success
     failure:(void (^)(NSURLSessionDataTask *operation,NSError *error))Error {
    
    // 创建任务管理
    AFHTTPSessionManager *sessionManger = [AFHTTPSessionManager manager];
    [sessionManger setSecurityPolicy:[self customSecurityPolicy]];
    
    [sessionManger POST:URL parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [self sucessAction:success task:task responseObject:responseObject];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        Error(task, error);
    }];
    
    
    
}

+ (void)GET:(NSString *)URL success:(void (^)(id response))success
    failure:(void (^)(NSURLSessionDataTask *operation,NSError *error))Error {
    
    AFHTTPSessionManager *sessionManger = [AFHTTPSessionManager manager];
    [sessionManger setSecurityPolicy:[self customSecurityPolicy]];

    [sessionManger GET:URL parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [self sucessAction:success task:task responseObject:responseObject];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        Error(task, error);
    }];
    
}

+ (void)GET:(NSString *)URL parameters:(NSDictionary *)parameters success:(void (^)(id response))success
    failure:(void (^)(NSURLSessionDataTask *operation,NSError *error))Error {
    
    AFHTTPSessionManager *sessionManger = [AFHTTPSessionManager manager];
    [sessionManger setSecurityPolicy:[self customSecurityPolicy]];

    [sessionManger GET:URL parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [self sucessAction:success task:task responseObject:responseObject];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        Error(task, error);
    }];
    
}


+ (void)sucessAction:(void(^)(id responseDic))success task:(NSURLSessionDataTask * _Nonnull)task responseObject:(id  _Nullable)responseObject {
    
    NSDictionary *responseDic;
    if ([responseObject  isKindOfClass:[NSData  class]]) {
        responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
    }else if([responseObject isKindOfClass:[NSString  class]]) {
        NSString *receiveStr = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSData * data = [receiveStr dataUsingEncoding:NSUTF8StringEncoding];
        responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    }
    success(responseObject);
}

- (void)dealloc {
    NSLog(@"GCZAFNetworking delloc");
}

@end
